package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.AreaBean;
import com.project.bean.ClassBean;
import com.project.bean.ConsultBean;
import com.project.bean.EventBean;
import com.project.dao.EventDao;
import com.project.service.AreaService;
import com.project.service.ClassService;
import com.project.service.ConsultService;
import com.project.service.EventService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 事件
 */

@Service
@Transactional
public class EventServiceImpl extends ServiceImpl<EventDao, EventBean> implements EventService {

    @Resource
    private ClassService classService;
    @Resource
    private AreaService areaService;
    @Resource
    private ConsultService consultService;
    /**
     * 分页显示所有事件记录
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<EventBean> findAllEvent(int pageNo, int pageSize) {
        Page<EventBean> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.findAllEvent(page);
    }

    /**
     * 添加事件
     *
     * @param event
     */
    @Override
    public void addEvent(EventBean event,int areaId) {
        this.baseMapper.addEvent(event,areaId);
    }

    /**
     * 根据事件id查询事件详细信息
     *
     * @param eventId
     * @return
     */
    @Override
    public EventBean findId(int eventId) {
        EventBean eventBean = this.baseMapper.findId(eventId);
//        if (eventBean.getConsultBeanList().get(0).getConResult()==null){
//            eventBean.getConsultBeanList().remove(0);
//        }
        AreaBean areaBean = areaService.findAreaName(eventBean.getAreaBean().getAreaName());
        ClassBean classBean =classService.findClass(areaBean.getAreaId());
        if (classBean!=null){
            eventBean.setClassName(classBean.getClassName());
        }else {
            eventBean.setClassName("暂无小班管理此区域");
        }

        QueryWrapper<ConsultBean> wrapper = new QueryWrapper<>();
        wrapper.eq("fk_eventId", eventId);
        List<ConsultBean> list = consultService.list(wrapper);
        if (list.size() != 0) {
            ConsultBean consultBean = list.get(list.size() - 1);
            eventBean.setAdvice(consultBean.getConResult());
        } else {
            eventBean.setAdvice("暂无专家建议，原因：专家还未开始会商");

        }
        return eventBean;
    }


    /**
     * 申请专家会商
     */
    @Override
    public void updateConsult(int eventId) {
        EventBean eventBean = new EventBean();
        eventBean.setEventId(eventId);
        eventBean.setEventStage("无法解决，申请专家会商");
        this.updateById(eventBean);
    }


    /**
     * 根据事件id去修改灾情状况、防治方案
     *
     * @param eventId
     * @param eventStage
     * @param eventControl
     */
    @Override
    public void updateEvent(int eventId, String eventStage, String eventControl) {
        if (eventStage != null && eventControl != null) {
            EventBean eventBean = new EventBean();
            eventBean.setEventId(eventId);
            eventBean.setEventStage(eventStage);
            eventBean.setEventControl(eventControl);
            this.updateById(eventBean);
        } else {
            EventBean eventBean = new EventBean();
            eventBean.setEventId(eventId);
            eventBean.setEventStage("无法解决，申请专家会商");
            this.updateById(eventBean);
        }

    }


    /**
     * 根据事件名称、灾情状况、发生位置、起始时间、结束时间模糊查询事件信息
     *
     * @param eventName
     * @param eventStage
     * @param address
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public IPage<EventBean> findName(int pageNo, int pageSize,
                                    String eventName, String eventStage,
                                    String address, LocalDate startTime, LocalDate endTime) {
        Page<EventBean> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.findName(page,eventName, eventStage, address, startTime, endTime);
    }


    /**
     * 根据事件id去将会商结果修改成专家建议
     * @param eventId
     * @param conResult
     * @param eventPro
     */
    @Override
    public void update(int eventId, String conResult, String eventPro) {
//        EventBean eventBean = new EventBean();
//        eventBean.setEventId(eventId);
//        eventBean.setEventPro(eventPro);
//        eventBean.getConsultBeanList()
//
//        ConsultBean consultBean = new ConsultBean();
//        consultBean.setConResult(eventPro);

//        this.updateById(eventBean,consultBean);
    }

    @Override
    public List<EventBean> findAllConsult() {
//        QueryWrapper<EventBean> wrapper = new QueryWrapper<>();
//        wrapper.eq("f_eventStage","无法解决，申请专家会商");
        return this.baseMapper.findAllConsult("无法解决，申请专家会商");
//        return this.list(wrapper);
    }
}
